﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.ViewQuery;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;

public partial class sysadmin_QuestionManager_BuilderPaper : PowerCreator.LMS.Web.Base.BasePage
{




    ManagerPaper objManagerPaper = new ManagerPaper();
    Course objCourse = new Course();
    ManagerQuestion objManagerQuestion = new ManagerQuestion();
    ViewQuery objViewQuery = new ViewQuery();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["PaperID"] = Request.QueryString["p"].ToString();
            ViewState["TypeID"] = Request.QueryString["type"].ToString();
            Pageload();
            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);
            InitPage();
        }
    }
    protected override void InitPage()
    {
        lab_TitleInfo.Text = GetLanageValueByKey("lab_BuilderPaper_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_PaperNameTitle.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_PaperCountTitle.Text = GetLanageValueByKey("lab_BuilderPaper_PaperCountQuestion", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_PaperScoreTitle.Text = GetLanageValueByKey("lab_BuilderPaper_PaperCountScore", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_KeguanCountTitle.Text = GetLanageValueByKey("lab_BuilderPaper_KeguanCountTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_ZhuguanCountTitle.Text = GetLanageValueByKey("lab_BuilderPaper_ZhuguanCountTitle", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_yulan.Text = GetLanageValueByKey("btn_BuilderPaper_Yulan", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_BuilderPaper.Text = GetLanageValueByKey("btn_BuilderPaper_BuildPaper", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_GoBack.Text = GetLanageValueByKey("btn_BuilderPaper_GoBack", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private string GetSearch()
    {
        string search = " and PaperQuestionTemp.FKPaperQuestionTempPaperID=" + ViewState["PaperID"].ToString();
        

        return search;
    }
    private void BinderPager(string search)
    {

        AspNetPager1.RecordCount = objManagerPaper.GetPaperTempQuestionListCount(search);
        AspNetPager1.PageSize = LMSPageSize;
    }

    private void BinderGV(string search, int pageIndex)
    {
        DataTable dt = new DataTable();

        dt = objManagerPaper.GetPaperTempQuestionList(search, LMSPageSize, pageIndex);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["PaperQuestionTempContent"] = StringUtils.GetSubstringLue(dt.Rows[i]["PaperQuestionTempContent"].ToString(), 15);
        }

        gvQuestionList.DataSource = dt;
        gvQuestionList.DataBind();

        PreAspNetPager(AspNetPager1);
    }
    private void Pageload()
    {
        int scorecount = 0;
        int keguanticount = 0;
        //PaperInfo info=new PaperInfo();
        VOPaper info = new VOPaper();
        info = objManagerPaper.LoadPaper(int.Parse(ViewState["PaperID"].ToString()));
        lab_Papername.Text = info.PaperName;

        DataTable dt = new DataTable();

        string search = GetSearch();

        dt = objManagerPaper.GetPaperTempQuestionList(search);
        
        for (int i=0; i < dt.Rows.Count; i++)
        {
            scorecount += int.Parse(dt.Rows[i]["PaperQuestionTempScore"].ToString());
            if (dt.Rows[i]["QuestionClass"].ToString() == "2")
            {
                keguanticount += 1;
            }
        }
        lab_zhuguancount.Text = Convert.ToString(dt.Rows.Count - keguanticount);
        lab_keguancount.Text = keguanticount.ToString();
        lab_paperScore.Text = scorecount.ToString(); 
        lab_questioncount.Text = dt.Rows.Count.ToString();
        //gvQuestionList.DataSource = dt;
        //gvQuestionList.DataBind();
        
        
    }
    protected void btn_BuilderPaper_Click(object sender, EventArgs e)
    {

        if (gvQuestionList.Rows.Count == 0)
        {
            Script.Alert(this, "msg", GetLanageValueByKey("msg_Question_Nodate", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }
        int paperID = int.Parse(ViewState["PaperID"].ToString());

        if (objManagerPaper.BuilderPaper(paperID))
        {
            Script.ShowAndRedirect(this, GetLanageValueByKey("msg_Question_paper_msg1", PowerCreator.LMS.Language.LanagetItemType.Page), "ManagerPaperInfo.aspx");
        }
        else
        {
            Script.ShowAndRedirect(this, GetLanageValueByKey("msg_Question_paper_msg2", PowerCreator.LMS.Language.LanagetItemType.Page), "ManagerPaperInfo.aspx");
        }
        
    }
    protected void gvQuestionList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "mydelete")
        {
            if (objManagerPaper.DeletePaperQuestionTemp(int.Parse(e.CommandArgument.ToString())))
            {
               
            }
            Pageload();
            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);
        }
        if (e.CommandName == "btn_Edit")
        {
            Script.OpenURL("EditPaperQuestion_Dialog.aspx?qid=" + e.CommandArgument.ToString());
            
            //Script.ShowModalDialog("EditPaperQuestion_Dialog.aspx?qid=" + e.CommandArgument.ToString(), "300", "200");
        }
    }
    protected void gvQuestionList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Text = GetLanageValueByKey("Msg_AddQuestionInfo_checkmsg4", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[2].Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionContent", PowerCreator.LMS.Language.LanagetItemType.Page);

            e.Row.Cells[3].Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionType", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[4].Text = GetLanageValueByKey("Msg_ManagerQuestion_QuestionScore", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[5].Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionDiffculty", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[6].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);
            //e.Row.Cells[8].Text = GetLanageValueByKey("btn_ManagerQuestion_QuestionState", PowerCreator.LMS.Language.LanagetItemType.Page);
            //e.Row.Cells[9].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);

        }
      
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("bgColor", "#F7FAFF");
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FAEDB8'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#F7FAFF'");

            switch (e.Row.Cells[3].Text.Trim())
            {
                case "1": e.Row.Cells[3].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Panduanti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "2": e.Row.Cells[3].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Danxuanti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "3": e.Row.Cells[3].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Duoxuanti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "4": e.Row.Cells[3].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Tiankongti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "5": e.Row.Cells[3].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Mingcijieshiti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "6": e.Row.Cells[3].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Jiandati", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "7": e.Row.Cells[3].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Zuheti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
            }
        } 
    }
    protected void btn_yulan_Click(object sender, EventArgs e)
    {
        string url = "ShowPaperInfoTemp.aspx?p=" + ViewState["PaperID"].ToString();
        Script.NewOpenURL(url);
    }
    protected void btn_GoBack_Click(object sender, EventArgs e)
    {
        string url = string.Empty;
        if (ViewState["TypeID"].ToString() == "1")
        {
            url = "SelectQuestion.aspx?p=" + ViewState["PaperID"].ToString();
        }
        else
        {
            url = "AoutSelectQuestion.aspx?p=" + ViewState["PaperID"].ToString();
        }
        Script.RedirectURL(url);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
}
